//*********************************************************
//
//    Copyright (c) Microsoft. All rights reserved.
//    This code is licensed under the Apache License, Version 2.0.
//    THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF
//    ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY
//    IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR
//    PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT.
//
//*********************************************************

using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.Unity;
using Microsoft.Research.ScientificWorkflow.UIDesigner.WpfComposite;
using System.Windows;
using System.Windows.Controls.Primitives;
using Microsoft.Research.ScientificWorkflow.ManagementStudioControls.Interfaces;
using Microsoft.Research.ScientificWorkflow.ManagementStudioControls.WorkflowManagerModule;
using Microsoft.Research.ScientificWorkflow.ManagementStudioControls.Common;
using System.Collections.ObjectModel;
using System.Windows.Input;

namespace Microsoft.Research.ScientificWorkflow.ManagementStudioControls
{
    /// <summary>
    /// This class represents the left pane control for the workflow manager module.
    /// </summary>
    internal class WorkflowManagerLeftPanePresenter : ILeftPaneControl
    {
        /// <summary>
        /// The unity container reference.
        /// </summary>
        private IUnityContainer container;

        /// <summary>
        /// The root level filters for the workflow manager.
        /// </summary>
        private ObservableCollection<WorkflowManagerFilterModel> filters = new ObservableCollection<WorkflowManagerFilterModel>();

        /// <summary>
        /// The presenters corresponding to the filters.
        /// </summary>
        private Dictionary<string, IListPresenter> workflowManagerListPresenters = new Dictionary<string, IListPresenter>();

        /// <summary>
        /// The currently selected list presenter.
        /// </summary>
        private IListPresenter currentSelectedPresenter;

        /// <summary>
        /// Constructor.
        /// </summary>
        /// <param name="container">The unity container reference.</param>
        /// <param name="view">The workflow manager left pane view.</param>
        /// <param name="mainContent">The centre pane.</param>
        public WorkflowManagerLeftPanePresenter(IUnityContainer container, WorkflowManagerView view, WorkflowManagerContentView mainContent)
        {
            this.ObjectName = "Workflow Manager";
            this.container = container;
            this.LeftPaneContent = view;
            this.MainContent = mainContent;
            view.Presenter = this;
            this.container.RegisterInstance(typeof(WorkflowManagerView), this.LeftPaneContent);
            this.FilterSelected = new DelegateCommand<WorkflowManagerFilterModel>(ExecuteFilterSelected);
        }

        /// <summary>
        /// The root level filters for the workflow manager.
        /// </summary>
        public ObservableCollection<WorkflowManagerFilterModel> Filters
        {
            get { return filters; }
        }

        /// <summary>
        /// The filter selected command.
        /// </summary>
        public ICommand FilterSelected { get; set; }

        /// <summary>
        /// Add a filter to the module.
        /// </summary>
        /// <param name="filter">filter root key.</param>
        /// <param name="listPresenter">The center pane presenter.</param>
        public void Add(WorkflowManagerFilterModel filter, IListPresenter listPresenter)
        {
            this.workflowManagerListPresenters.Add(filter.RootKey, listPresenter);
            this.filters.Add(filter);
        }

        /// <summary>
        /// The Execute delegate method for the Filter selected command.
        /// </summary>
        /// <param name="selectedFilter">The filter selected.</param>
        private void ExecuteFilterSelected(WorkflowManagerFilterModel selectedFilter)
        {
            if (currentSelectedPresenter != null)
            {
                this.currentSelectedPresenter.Items.Clear();
            }

            IListPresenter listPresenter = workflowManagerListPresenters[selectedFilter.RootKey];
            listPresenter.DisplayItems(selectedFilter.Filter);
            (this.MainContent as WorkflowManagerContentView).AddListView(listPresenter.ListView);
            (this.LeftPaneContent as WorkflowManagerView).AddToSearchPane(listPresenter.SearchView);
            this.currentSelectedPresenter = listPresenter;
        }

        #region ILeftPaneControl Members

        /// <summary>
        /// The left pane content for the module.
        /// </summary>
        public UIElement LeftPaneContent { get; set; }

        /// <summary>
        /// The center pane content for the module.
        /// </summary>
        public UIElement MainContent { get; set; }

        /// <summary>
        /// The button to select the module.
        /// </summary>
        public ToggleButton Selector { get; set; }

        /// <summary>
        /// The string containing the object name.
        /// </summary>
        public string ObjectName { get; set; }

        /// <summary>
        /// Saves the changes on the view.
        /// </summary>
        /// <returns>
        /// Value indicating success or failure of the save.
        /// </returns>
        public SaveStateHandler SaveChanges()
        {
           return new SaveStateHandler(SaveStatus.Valid);
        }

        #endregion
    }
}
